package com.ujs.edu.demo.controller;


import com.ujs.edu.demo.config.NeedLogin;
import com.ujs.edu.demo.dao.BcAdminMapper;
import com.ujs.edu.demo.dao.BcBooksMapper;
import com.ujs.edu.demo.dao.BcCollectMapper;
import com.ujs.edu.demo.dao.BcCommentMapper;
import com.ujs.edu.demo.dataobject.BcAdmin;
import com.ujs.edu.demo.dataobject.BcBooks;
import com.ujs.edu.demo.dataobject.BcCollect;
import com.ujs.edu.demo.dataobject.BcComment;
import com.ujs.edu.demo.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
public class BookDetailController {

    @Autowired
    private BcCommentMapper bcCommentMapper;
    @Autowired
    private BcBooksMapper bcBooksMapper;
    @Autowired
    private BcAdminMapper bcAdminMapper;
    @Autowired
    private BcCollectMapper bcCollectMapper;


@RequestMapping(path = "bookdetail")
    public ModelAndView bookDetail(@RequestParam("bookId") Integer bookId){

        ModelAndView view=new ModelAndView();

        BcBooks bcBooks=bcBooksMapper.selectByPrimaryKey(bookId);
        if (bcBooks!=null){
            view.addObject("book",bcBooks);
        }

        List<BcComment> bcComments=bcCommentMapper.selectByBookId(bookId);

        if (bcComments.size()!=0){
            view.addObject("comments",bcComments);
        }

        view.setViewName("bookdetail");
        return view;
    }


    //写评论
    @RequestMapping(path = "comment/post")
    public String comment(BcComment bcComment,@SessionAttribute("bcAdmin")BcAdmin bcAdmin1){

        BcAdmin bcAdmin=bcAdminMapper.selectByPrimaryKey(bcAdmin1.getAdminId());
        if (bcAdmin!=null){
            bcComment.setMemberId(bcAdmin.getAdminId());
            bcComment.setMemberimage(bcAdmin.getUsername());
            bcComment.setMembername(bcAdmin.getUsername());
        }

        int co=bcCommentMapper.insert(bcComment);
        if (co==0){
            return "redirect:/test";
        }

        return "redirect:/bookdetail?bookId="+bcComment.getBookId();
    }

    //加入书架
    @RequestMapping(path = "addstore")
    @ResponseBody
    public Result addstore(@SessionAttribute("bcAdmin") BcAdmin bcAdmin,@RequestParam("bookId") Integer bookId){
        Result result=new Result();

        BcCollect bcCollect=new BcCollect();
        bcCollect.setBookid(bookId);
        bcCollect.setMemberid(bcAdmin.getAdminId());

        int bc=bcCollectMapper.insert(bcCollect);

        if(bc==1){
            result.setStatus(1);
            result.setMsg("添加成功");
        }else {
            result.setStatus(0);
            result.setMsg("添加失败");
        }

        return result;
    }

    //进入个人中心
    @RequestMapping(path = "personcenter")
    public ModelAndView personcenter(@SessionAttribute("bcAdmin") BcAdmin bcAdmin){
        List<BcCollect>bcCollects=bcCollectMapper.selectByMany();
        ModelAndView view=new ModelAndView();

        List<BcBooks>books=new ArrayList<>();

        for (int i=0;i<bcCollects.size();i++){
            if (bcCollects.get(i).getMemberid()==bcAdmin.getAdminId()){
                books.add(bcCollects.get(i).getBooks().get(0));
            }

        }

        view.addObject("collects",books);

        view.setViewName("personcenter");

        return view;
    }
}
